﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import flash.events.*;
    import Engine.Events.*;
    import Engine.Managers.*;
    import Engine.Helpers.*;
    import Engine.*;

    public class CloudManager {

        private static const m_defaultCloudHue:uint = 0xFFFFFF;
        private static const DEBUG_MODE:Boolean = false;
        private static const ZOOM_CLOSEST:int = 4;
        private static const ZOOM_MIDDLE:int = 3;
        private static const ZOOM_FARTHEST:int = 2;

        private static var m_instance:CloudManager;
        private static var blnNotInit:Boolean = true;
        private static var blnLoadNotDone:Boolean = true;
        private static var m_CloudSystemActivated:Boolean = true;
        private static var m_Clouds:Array;
        private static var m_AmbientCloudsFlag:int = 1;
        private static var m_yMin:Number = 1;
        private static var m_yMax:Number = 1;
        private static var m_xMin:Number = 1;
        private static var m_xMax:Number = 1;
        private static var m_CloudSpawnYMin:Number = 1;
        private static var m_CloudSpawnYMax:Number = 1;
        private static var m_CloudSpawnXMin:Number = 1;
        private static var m_pathSlope:Number = 1;
        private static var m_DensityMin:int = 1;
        private static var m_DensityMax:int = 1;
        private static var m_SpeedMin:Number = 1;
        private static var m_SpeedMax:Number = 1;
        private static var m_SizeMin:Number = 1;
        private static var m_SizeMax:Number = 1;
        private static var m_opacityZoom1:Number = 1;
        private static var m_DensityMin_Zoom1:int = 1;
        private static var m_DensityMax_Zoom1:int = 1;
        private static var m_opacityZoom2:Number = 1;
        private static var m_DensityMin_Zoom2:int = 1;
        private static var m_DensityMax_Zoom2:int = 1;
        private static var m_opacityZoom3:Number = 1;
        private static var m_DensityMin_Zoom3:int = 1;
        private static var m_DensityMax_Zoom3:int = 1;
        private static var m_cloudTimePeriodMin:int = 1;
        private static var m_cloudTimePeriodMax:int = 1;
        private static var m_CloudDelayTime:Number = -1;
        private static var m_CloudTimerStart:Number = -1;
        private static var m_cloudHues:Array;

        private static function CheckInit():void{
            if (blnNotInit){
                OnInit();
                blnNotInit = false;
            };
        }
        public static function getInstance():CloudManager{
            if (m_instance == null){
                m_instance = new (CloudManager)();
            };
            return (m_instance);
        }
        public static function StartUp():void{
            CheckInit();
        }
        private static function OnInit():void{
            m_Clouds = new Array();
            m_AmbientCloudsFlag = Global.gameSettings.getInt("ambientClouds");
            m_yMin = Global.gameSettings.getNumber("cloudYMin");
            m_yMax = Global.gameSettings.getNumber("cloudYMax");
            m_xMin = Global.gameSettings.getNumber("cloudXMin");
            m_xMax = Global.gameSettings.getNumber("cloudXMax");
            m_CloudSpawnYMin = Global.gameSettings.getNumber("cloudSpawnYMin");
            m_CloudSpawnYMax = Global.gameSettings.getNumber("cloudSpawnYMax");
            m_CloudSpawnXMin = Global.gameSettings.getNumber("cloudSpawnXMin");
            m_pathSlope = Global.gameSettings.getNumber("cloudPathSlope");
            m_SpeedMin = Global.gameSettings.getNumber("cloudSpeed_Min");
            m_SpeedMax = Global.gameSettings.getNumber("cloudSpeed_Max");
            m_SizeMin = Global.gameSettings.getNumber("cloudSize_Min");
            m_SizeMax = Global.gameSettings.getNumber("cloudSize_Max");
            m_opacityZoom1 = Global.gameSettings.getNumber("opacityZoom1");
            m_opacityZoom2 = Global.gameSettings.getNumber("opacityZoom2");
            m_opacityZoom3 = Global.gameSettings.getNumber("opacityZoom3");
            m_DensityMin_Zoom1 = Global.gameSettings.getInt("cloudDensity_Min_Zoom1");
            m_DensityMin_Zoom2 = Global.gameSettings.getInt("cloudDensity_Min_Zoom2");
            m_DensityMin_Zoom3 = Global.gameSettings.getInt("cloudDensity_Min_Zoom3");
            m_DensityMax_Zoom1 = Global.gameSettings.getInt("cloudDensity_Max_Zoom1");
            m_DensityMax_Zoom2 = Global.gameSettings.getInt("cloudDensity_Max_Zoom2");
            m_DensityMax_Zoom3 = Global.gameSettings.getInt("cloudDensity_Max_Zoom3");
            m_cloudTimePeriodMin = Global.gameSettings.getInt("cloudTimePeriodMin");
            m_cloudTimePeriodMax = Global.gameSettings.getInt("cloudTimePeriodMax");
            var _local1:String = Global.gameSettings.getString("cloudHue");
            m_cloudHues = _local1.split(":");
            LoadingManager.getInstance().addEventListener(LoaderEvent.LOAD_QUEUE_EMPTY, getInstance().assetLoadCompleted);
        }
        public static function get activated():Boolean{
            return (m_CloudSystemActivated);
        }
        public static function set activated(_arg1:Boolean):void{
            if (((m_CloudSystemActivated) && (!(_arg1)))){
                turnOffClouds();
            };
            if (((!(m_CloudSystemActivated)) && (_arg1))){
                turnOnClouds();
            };
            m_CloudSystemActivated = _arg1;
            CloudManager.updateZoomFactor(GlobalEngine.viewport.getZoom());
        }
        public static function toggleActivation():void{
            if (activated){
                activated = false;
            } else {
                activated = true;
            };
        }
        private static function turnOffClouds():void{
        }
        private static function turnOnClouds():void{
        }
        public static function addCloud(_arg1:Cloud):void{
            CheckInit();
            m_Clouds.push(_arg1);
        }
        public static function removeCloud(_arg1:Cloud):void{
            var _local2 = -1;
            var _local3:int;
            while (_local3 < m_Clouds.length) {
                if (m_Clouds[_local3] == _arg1){
                    _local2 = _local3;
                    _local3 = m_Clouds.length;
                };
                _local3++;
            };
            Global.world.citySim.npcManager.removeCloud(_arg1);
            if (_local2 > -1){
                m_Clouds.splice(_local2, 1);
            };
        }
        private static function pickInitialCloudNumber():int{
            return (Math.round(Utilities.randBetween(m_DensityMin, m_DensityMax)));
        }
        private static function pickSpeed():Number{
            return (Utilities.randBetween(m_SpeedMin, m_SpeedMax));
        }
        private static function pickSize():Number{
            return (Utilities.randBetween(m_SizeMin, m_SizeMax));
        }
        private static function pickX():Number{
            return (Utilities.randBetween(m_xMin, m_xMax));
        }
        private static function pickY():Number{
            return (Utilities.randBetween(m_yMin, m_yMax));
        }
        private static function pickCloudHue():uint{
            var _local1:int = Math.round(Utilities.randBetween(0, (m_cloudHues.length - 1)));
            if (m_cloudHues[_local1].length == 0){
                return (m_defaultCloudHue);
            };
            return (parseInt(m_cloudHues[_local1], 16));
        }
        private static function pickRightScreenPercent():Number{
            return ((Utilities.randBetween(m_CloudSpawnYMin, m_CloudSpawnYMax) / 100));
        }
        private static function getCloudAlpha():Number{
            if (!m_CloudSystemActivated){
                return (0);
            };
            var _local1:Number = GlobalEngine.viewport.getZoom();
            switch (_local1){
                case ZOOM_CLOSEST:
                    return (m_opacityZoom1);
                case ZOOM_MIDDLE:
                    return (m_opacityZoom2);
                case ZOOM_FARTHEST:
                    return (m_opacityZoom3);
                default:
                    return (0);
            };
        }
        private static function getDensityMin():int{
            if (!m_CloudSystemActivated){
                return (0);
            };
            var _local1:Number = GlobalEngine.viewport.getZoom();
            switch (_local1){
                case ZOOM_CLOSEST:
                    return (m_DensityMin_Zoom1);
                case ZOOM_MIDDLE:
                    return (m_DensityMin_Zoom2);
                case ZOOM_FARTHEST:
                    return (m_DensityMin_Zoom3);
                default:
                    return (0);
            };
        }
        private static function getDensityMax():int{
            if (!m_CloudSystemActivated){
                return (0);
            };
            var _local1:Number = GlobalEngine.viewport.getZoom();
            switch (_local1){
                case ZOOM_CLOSEST:
                    return (m_DensityMax_Zoom1);
                case ZOOM_MIDDLE:
                    return (m_DensityMax_Zoom2);
                case ZOOM_FARTHEST:
                    return (m_DensityMax_Zoom3);
                default:
                    return (0);
            };
        }
        private static function spawnCloud():void{
            var _local3:Cloud;
            var _local1:Boolean = Global.player.npc_cloud_visible;
            if (!_local1){
                return;
            };
            var _local2:Number = pickRightScreenPercent();
            _local2 = (1 - _local2);
            var _local4:Number = Global.stage.y;
            var _local5:Number = Global.stage.stageHeight;
            var _local6:Number = Global.stage.x;
            var _local7:Number = Global.stage.stageWidth;
            var _local8:Number = (_local7 + _local6);
            var _local9:Number = (_local4 + (_local5 * _local2));
            var _local10:Vector2 = IsoMath.screenPosToTilePos(_local8, _local9);
            var _local11:Number = pickSpeed();
            var _local12:Number = pickSize();
            if (DEBUG_MODE){
                _local11 = (_local11 * 20);
            };
            var _local13:Array = new Array();
            var _local14:Number = ((m_pathSlope * (m_yMax - _local10.y)) + _local10.x);
            _local13.push(new Vector3(_local14, m_yMax, 0));
            _local3 = Global.world.citySim.npcManager.createCloudByNameAtPosition("cloud_NPC", new Vector3(_local10.x, _local10.y, 0), _local13, false);
            _local3.velocityWalk = _local11;
            _local3.velocityRun = _local11;
            _local3.cloudScale = _local12;
            _local3.setColorOverrideHue(pickCloudHue());
            _local3.alpha = getCloudAlpha();
            addCloud(_local3);
        }
        public static function spawnInitialClouds():void{
            CheckInit();
            if (!m_CloudSystemActivated){
                return;
            };
        }
        public static function updateZoomFactor(_arg1:Number):void{
            var _local3:Cloud;
            var _local4:Number;
            var _local2:int;
            while (_local2 < m_Clouds.length) {
                _local3 = m_Clouds[_local2];
                _local4 = getCloudAlpha();
                _local3.alpha = _local4;
                _local3.setVisible((_local4 > 0));
                _local2++;
            };
        }
        public static function respawnCloud():void{
            CheckInit();
            spawnCloud();
        }
        private static function generateCloudTimePeriod():void{
            m_CloudDelayTime = Math.round(Utilities.randBetween(m_cloudTimePeriodMin, m_cloudTimePeriodMax));
            m_CloudDelayTime = (m_CloudDelayTime * 1000);
            if (DEBUG_MODE){
                m_CloudDelayTime = (m_CloudDelayTime / 20);
            };
        }
        private static function resetCloudTimer():void{
            m_CloudTimerStart = GlobalEngine.getTimer();
        }
        public static function doTimerCheck():void{
            if (blnNotInit){
                return;
            };
            if (blnLoadNotDone){
                return;
            };
            if (!m_CloudSystemActivated){
                return;
            };
            if (m_CloudDelayTime == -1){
                generateCloudTimePeriod();
            };
            var _local1:Boolean;
            var _local2:Number = (GlobalEngine.getTimer() - m_CloudTimerStart);
            if ((((_local2 > m_CloudDelayTime)) || ((m_Clouds.length == 0)))){
                _local1 = true;
            };
            if (_local1 == true){
                cloudTimerTick();
                resetCloudTimer();
            };
        }
        private static function cloudTimerTick():void{
            var _local7:Number;
            var _local1:int = getDensityMin();
            var _local2:int = getDensityMax();
            if (m_Clouds.length == 0){
                spawnCloud();
                return;
            };
            if (m_Clouds.length >= _local2){
                return;
            };
            if (m_Clouds.length < _local1){
                _local7 = Math.round(Utilities.randBetween(0, 100));
                if (_local7 >= 50){
                    spawnCloud();
                };
                return;
            };
            var _local3:Number = (_local2 - _local1);
            var _local4:Number = (_local2 - m_Clouds.length);
            var _local5:Number = ((_local2 - m_Clouds.length) / ((_local4 + 1) * 2));
            var _local6:Number = Utilities.randBetween(0, 1);
            if (_local6 <= _local5){
                spawnCloud();
            };
        }

        protected function assetLoadCompleted(_arg1:Event):void{
            blnLoadNotDone = false;
            LoadingManager.getInstance().removeEventListener(LoaderEvent.LOAD_QUEUE_EMPTY, getInstance().assetLoadCompleted);
            CloudManager.spawnInitialClouds();
        }

    }
}//package Classes 
